package com.pink;

import java.awt.*;

public class TinyEggPrinter implements EggProducer {
    private RobotEx robot;
    private Integer timeout;

    public TinyEggPrinter(RobotEx robotEx) {
        this.robot = robotEx;
        this.timeout=0;
    }
    public boolean eggUpdate(){

        return this.eggCore(1143,675,3000);
    }

    @Override
    public boolean eggBattle() {
       return this.eggCore(1182,511,13000);
    }
    private boolean eggCore(int x,int y ,int delaytimems ){
        robot.delay(1000);
//            105 挑战按钮
        robot.moveAndClick(x,y);
//        robot.moveAndClick(1182, 511);
        // 1038 658　开始挑战按钮　
        robot.moveAndClick(1038, 658);
        robot.delay(1000);

//          跳转到对战开始页面
        robot.moveAndClick(1000, 350);
//            robot.moveAndClick(977, 351);
// 检测模块　开始自动战役　观测点与颜色
//              java.awt.Point[x=714,y=526]
//              java.awt.Color[r=103,g=36,b=20]
//        robot.delay(1000);
//        　看是否体力不足　应该停下
//            java.awt.Point[x=922,y=546]
//      java.awt.Color[r=230,g=233,b=236
        if (robot.getPixelColor(922, 546).equals(new Color(230, 233, 236))) {
//                System.out.println("逐鹿体力已经不足，今日" + username + "已经刷了" + i + "个体力");
//                analysis.append(username+"已经刷了" + i + "个逐鹿体力");
//                点俩关闭按钮
            robot.moveAndClick(1012, 292);
            robot.moveAndClick(1143, 229);
            return false;
        }
// 核心模块
        robot.delay(delaytimems);
        while (true) {
            robot.delay(2000);
//                刷逐鹿的核心监测点　714 526
            Color dot = robot.getPixelColor(714, 526);
//                System.out.println(dot);

//              r=172,g=150,b=120 ==>  java.awt.Color[r=103,g=36,b=20]
//            if ((dot.getGreen() < 40) && (dot.getBlue() < 25)) break;
                if (dot.equals(new Color(103,36,20))){
                    robot.delay(1000);
                    break;
                }
        }
        robot.moveAndClick(750, 550);
        robot.moveAndClick(760,550);
//            java.awt.Color[r=255,g=242,b=125]
//52 : java.awt.Point[x=759,y=689]
        Color color = new Color(253, 225, 110);
        while (true){
            Color reward = robot.getPixelColor(763, 686);
            System.out.println(reward);
            if (reward.equals(color)){
                robot.moveAndClick(763, 686);
//                System.out.println("已经点击了"+reward);
                break;
            }else {

                robot.click(2000);
//                System.out.println("等待测试一");
                timeout++;
                if (timeout>=3){
                    timeout=0;
                    throw new RuntimeException("本次逐鹿进行了但是检测出问题了");
                }
            }

        }
        timeout=0;
        robot.moveAndClick(1000, 400);
//        todo:
//增加确认刷完返回到逐鹿页面的确认　java.awt.Point[x=1058,y=681]
//java.awt.Color[r=49,g=148,b=44]
//        如果不是就每个ｎ秒反复点击确认按钮　robot.moveAndClick(763, 686);
        while (true){
            Color p = robot.getPixelColor(1058, 681);
            if (p.equals(new Color(49,148,44))){
                break;
            }
            robot.moveAndClick(763, 686);
            robot.delay(2000);
            System.out.println("sorry .没有返回逐鹿主页面　再次尝试　无限次尝试");
        }

        return true;
    }
    //        [x=350,y=443]
//java.awt.Color[121,31,127
    public static final Color dailyWarColor1=new Color(121,31,127);
    //        java.awt.Point[x=396,y=251]
//java.awt.Color[r=144,g=48,b=34]
    public static final Color dailyWarColor2=new Color(144,48,34);
    //        java.awt.Point[x=296,y=227]
//java.awt.Color[r=53,g=44,b=39]
    public static final Color dailyWarColor3=new Color(53,44,39);
//    668 625 灰色确定按钮
    public static final Color dailyWarColor4=new Color(133, 133, 133);
    @Override
    public void dailyWar() {
        robot.delay(1000);
//        robot.moveAndClick();
//        [x=350,y=443]
//java.awt.Color[r=116,g=30,b=122
//        while (true){
//            if (robot.getPixelColor(350,443).equals(dailyWarColor1)){
//                break;
//            }
//            robot.delay(3000);
//        }
//        robot.moveAndClick(350,443);
        robot.waitThenClick(350,443,dailyWarColor1);

        robot.moveAndClick(1172,671);
//        java.awt.Point[x=396,y=251]
//java.awt.Color[r=144,g=48,b=34]
        robot.click(2000);
        robot.waitThenClick(396,251,dailyWarColor2);
//[x=297,y=421]
//java.awt.Color[r=72,g=161,b=69]
// 一局结束后的福利弹出框
//        1146 233  rgb :  160 110 64
//        java.awt.Point[x=296,y=227]
//java.awt.Color[r=53,g=44,b=39]
        robot.keepClickUntil(296,227,dailyWarColor3);
//        668 625  133 133 133
        robot.keepClickUntil(668,625,dailyWarColor4);
//        139,160,93
//        点击自杀按钮
        for (int i = 0; i < 5; i++) {

            robot.moveAndClickQuick(1065,698);
        }
//        点击取消按钮
        robot.moveAndClick(812,628);
//        t[x=654,y=684]
//java.awt.Color[r=255,g=246,b=235]
//        点击关闭
        robot.waitThenClick(654,684,new Color(255,246,235));
//        空闲处点击
        robot.moveAndClick(200,700);
//        点击黄色继续按钮
        robot.waitThenClick(741,684,new Color(253,226,106));
//        if ()
//        [x=530,y=304]
//java.awt.Color[r=239,g=228,b=217]
//        前十次有领奖银两
        if(robot.getPixelColor(530,304).equals(new Color(239,228,217))){
//            翻开卡牌
            robot.moveAndClick(562,477);
//            点击继续
            robot.moveAndClick(750,686);
        }
        robot.delay(2000);
        if (robot.getPixelColor(1145,232).equals(new Color(169,122,71))){
            robot.moveAndClick(1145,232);
        }
    }


}
